package com.fernfx.xingtan.main.ui;

import android.content.Intent;
import android.support.constraint.ConstraintLayout;
import android.text.TextUtils;
import android.util.EventLog;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.fernfx.xingtan.Constant;
import com.fernfx.xingtan.R;
import com.fernfx.xingtan.common.base.BaseActivity;
import com.fernfx.xingtan.common.base.BaseFragment;
import com.fernfx.xingtan.common.photo.PhotoActivity;
import com.fernfx.xingtan.main.adapter.AddImgAdapter;
import com.fernfx.xingtan.main.contract.RobredPacketContract;
import com.fernfx.xingtan.main.entity.SelectLocationResultEntity;
import com.fernfx.xingtan.main.presenter.RobredPacketPresenter;
import com.fernfx.xingtan.utils.CollectionUtil;
import com.fernfx.xingtan.utils.OtherUtil;
import com.fernfx.xingtan.utils.SoftInputUtil;
import com.fernfx.xingtan.utils.ToastUtil;
import com.fernfx.xingtan.view.listview.HorizontalListView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

import static com.fernfx.xingtan.Constant.ApplicationVariable.MAX_ROBRED_PACKET_COUNT;

/**
 * Created by admin on 2018/8/10.
 */

public class RobredPacketActivity extends PhotoActivity implements View.OnClickListener, RobredPacketContract.View {
    @BindView(R.id.tv_refund_project_title)
    TextView refundTitleTv;
    @BindView(R.id.upload_image_iv)
    ImageView uploadImageIv;
    @BindView(R.id.add_img_toast_tv)
    TextView addImgToastTv;
    @BindView(R.id.send_select_scope_tv)
    TextView selectScopeTv;
    @BindView(R.id.upload_img_liv)
    HorizontalListView uploadImgLiv;
    @BindView(R.id.input_money_edit)
    EditText inputMoneyEdit;
    @BindView(R.id.money_count_edit)
    EditText moenyCountEdit;

    public static final int ROBRED_PACKET_RESULT_CODE = 11;
    //presenter
    private RobredPacketContract.Presenter presenter = null;
    //上传的图片路径
    private String uploadImgPath;
    //经纬度
    private String latitudeText,longitudeText="";
    //状态默认是q城市
    private int range = 1;
    public static final String CITY_TEXT = "city";
    //images list
    private List<String> imagesList = new ArrayList<>();
    //添加图片的adapter
    private AddImgAdapter addImgAdapter;

    public static void start(BaseFragment baseFragment,String city) {
        Intent intent = new Intent(baseFragment.getActivity(), RobredPacketActivity.class);
        intent.putExtra(CITY_TEXT,city);
        baseFragment.startActivityForResult(intent, ROBRED_PACKET_RESULT_CODE);
    }

    @Override
    protected void initView() {
        refundTitleTv.setVisibility(View.VISIBLE);
        refundTitleTv.setText("记录");
        setToolbarTitle("发红包");
        //init presenter
        presenter = new RobredPacketPresenter();
        presenter.init(this);
        //init adapter
        addImgAdapter = new AddImgAdapter(mContext,imagesList);
        uploadImgLiv.setAdapter(addImgAdapter);
    }

    @Override
    protected void initData() {
        //获取七牛token
        presenter.getQiniuToken();
        //图片上传成功之后的回调
        uploadSucessCallback = new UploadSucessCallback() {
            @Override
            public void request(String imgPath) {
                imagesList.add(Constant.ApplicationVariable.QI_NIU_IMG_PATH+imgPath);
                addImgAdapter.notifyDataSetChanged();
            }
        };

        //注册
        EventBus.getDefault().register(this);
        //init city
        latitudeText = getIntent().getStringExtra(CITY_TEXT);
    }

    @Override
    public void setQiniuToken(final String token) {
        if (TextUtils.isEmpty(token)) {
            ToastUtil.showCentertoast("七牛token为空");
            return;
        }
        //重新编码并设置
        this.token = token;
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_robred_packet;
    }

    @OnClick({R.id.robred_packet_clt, R.id.tv_refund_project_title, R.id.add_img_lat,R.id.select_scope_rlt,R.id.send_robred_packet_bt})
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.robred_packet_clt:
                SoftInputUtil.showSoftInput(mContext);
                break;
            case R.id.tv_refund_project_title:
                RobredPacketHistroyActivity.start(this);
                break;
            case R.id.add_img_lat:
                if (imagesList.size() >= MAX_ROBRED_PACKET_COUNT) {
                    ToastUtil.showCentertoast("最大只允许上传一张图片");
                    return;
                }
                presenter.showUserLogoDialog(mContext);
                break;
            case R.id.select_scope_rlt:
                SelectLocationActivity.start(this);
                break;
            case R.id.send_robred_packet_bt:
                String inputMoney = trimEditText(inputMoneyEdit);
                //检验用户输入的金额是否合法
                if (TextUtils.isEmpty(inputMoney) || TextUtils.equals("0",inputMoney)) {
                    ToastUtil.showCentertoast("总金额不能为空,获取不能为0");
                    return;
                }
                //检验用户输入的钱包个数是否合法
                String moneyCount = trimEditText(moenyCountEdit);
                if (TextUtils.isEmpty(moneyCount) || TextUtils.equals("0",moneyCount)) {
                    ToastUtil.showCentertoast("红包个数不能为空,获取不能为0");
                    return;
                }

                break;
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void setSLocationResult(SelectLocationResultEntity sLocationResult) {
        latitudeText = sLocationResult.getLatitudeText();
        longitudeText = sLocationResult.getLongitudeText();
        range = sLocationResult.getRange();
        //选择的城市名
        String selectText = sLocationResult.getSelectText();
        selectScopeTv.setText(TextUtils.isEmpty(selectText) ? "全市" : selectText);

    }

    @Override
    public BaseActivity getActivity() {
        return this;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //unregister EventBus
        EventBus.getDefault().unregister(this);
        if (null != presenter) {
            presenter.detachView();
            presenter = null;
        }
        if (!CollectionUtil.isEmpty(imagesList)) {
            imagesList.clear();
            imagesList = null;
        }
        if (null != addImgAdapter) {
            addImgAdapter = null;
        }
    }
}
